import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../index.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

/*欢迎页*/
class PreviewPhotoPage extends StatefulWidget {
  late List<TyPhoto> photos;
  late int index;

  PreviewPhotoPage({Key? key, required this.photos, this.index = 0})
      : super(key: key);

  @override
  _PreviewPhotoState createState() => _PreviewPhotoState();
}

class _PreviewPhotoState extends State<PreviewPhotoPage> {
  late PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: widget.index);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text("图片预览"),
          centerTitle: true,
        ),
        body: PhotoViewGallery.builder(
          scrollPhysics: const BouncingScrollPhysics(),
          builder: (BuildContext context, int index) {
            TyPhoto photo = widget.photos[index];
            ImageProvider imageProvider;
            if (photo.file != null) {
              imageProvider = AssetEntityImageProvider(photo.file!, isOriginal: true);
            } else {
              imageProvider = NetworkImage(photo.url!);
            }
            return PhotoViewGalleryPageOptions(
              imageProvider: imageProvider,
              initialScale: PhotoViewComputedScale.contained,
              heroAttributes: PhotoViewHeroAttributes(tag: index),
            );
          },
          itemCount: widget.photos.length,
          loadingBuilder: (context, event) => Center(
            child: SizedBox(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(
                value: event == null || event.expectedTotalBytes == null
                    ? 0
                    : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
              ),
            ),
          ),
          backgroundDecoration: const BoxDecoration(
            color: Colors.black,
          ),
          pageController: pageController,
          // onPageChanged: onPageChanged,
        ));
  }
}
